<template>
  <div class="total-box">
    <!-- 左侧树结构 -->
    <div class="head-container">
      <el-tree
        ref="tree"
        style="height: 100%"
        :data="deptOptions"
        :props="defaultProps"
        :expand-on-click-node="false"
        highlight-current
        node-key="id"
        :default-expanded-keys="defaultExpandedKeys"
        @node-click="handleNodeClick"
      >
        <template slot-scope="{node,data}">
          <span class="el-tree-node_box" :title="data.name">{{ data.name }}</span>
        </template>
      </el-tree>
    </div>

    <!-- 右侧 -->
    <div class="app-container">
      <el-row :gutter="20" style="height: 100%">
        <!--用户数据-->
        <el-col :span="24" :xs="24">
          <HkSearchform
            :data="queryParams"
            label-width="180px"
            :field-list="filterInfo.fieldList"
            :list-type-info="dictList"
            @handleSearch="handleQuery"
            @handleReset="resetQuery"
            @handleEvent="handleEvent"
          />

          <el-row :gutter="10" style="margin-left:0">
            <el-button size="mini" @click="handleExport">导出 </el-button>
          </el-row>

          <hk-table
            ref="tableRef"
            :loading="loading"
            :table-data="tableData"
            :table-columns="tableList"
            :selection="false"
            :show-index="true"
            :total="total"
            :query-params="queryParams"
            :operate="true"
            :table-height="80"
            :common-fixed="true"
            :operate-width="200"
            @handleSelectionChange="handleSelectionChange"
            @changePage="getList"
          >
            <template #operate="scope">
              <el-button size="mini" type="text" @click="handleView(scope.row)">详情 </el-button>
            </template>
          </hk-table>
        </el-col>
      </el-row>
    </div>
    <host ref="host" :title="title" :sub-btn="subBtn" :dict-list="dictList" />
    <server ref="server" :title="title" :sub-btn="subBtn" :dict-list="dictList" />
    <netDevice ref="netDevice" :title="title" :sub-btn="subBtn" :dict-list="dictList" />
    <secProduct ref="secProductRef" :title="title" :sub-btn="subBtn" :dict-list="dictList" />
    <storageMedium ref="storageMedium" :title="title" :sub-btn="subBtn" :dict-list="dictList" />
    <outsideeEquipment ref="outsideeEquipment" :title="title" :sub-btn="subBtn" :dict-list="dictList" />
    <otherEquipment ref="otherEquipment" :title="title" :sub-btn="subBtn" :dict-list="dictList" />
    <softwareSystem ref="softwareSystem" :title="title" :sub-btn="subBtn" :dict-list="dictList" />
  </div>
</template>

<script>
import { entityTree } from '@/api/assets/org'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import HkTable from '@/components/Hk-Table/index.vue'
import { listAll, queryPageList } from '@/api/assets/assetsAll'

import HkSearchform from '@/components/HkSearchform/index.vue'
import DictLabel from '@/components/Common/dictLabel.vue'

import host from '../host/component/emitDialog.vue'
import server from '../server/component/emitDialog.vue'
import netDevice from '../net/component/emitDialog.vue'
import secProduct from '../secProduct/component/emitDialog.vue'
import storageMedium from '../storageMedium/component/emitDialog.vue'
import outsideeEquipment from '../outside/component/emitDialog.vue'
import otherEquipment from '../other/component/emitDialog.vue'
import softwareSystem from '../../software/component/emitDialog.vue'
import { treeselectStorageSystem } from '@/api/assets/softwareSystem'
import { queryLoadBalancing, queryServer, treeselectStorage } from '@/api/assets/secProduct'

export default {
  components: {
    Treeselect,
    HkTable,
    HkSearchform,
    DictLabel,
    host,
    server,
    netDevice,
    secProduct,
    storageMedium,
    outsideeEquipment,
    otherEquipment,
    softwareSystem
  },
  data() {
    return {
      defaultExpandedKeys: ['root'],
      // 搜索项配置
      filterInfo: {
        fieldList: [
          { label: '关键字', type: 'input', field: 'keyword' },
          {
            label: '资产类型',
            type: 'select',
            field: 'assetsType',
            list: 'assetsAllTypeList'
          },
          {
            label: '使用状态',
            type: 'select',
            field: 'equipmentUseStatus',
            list: 'equipmentUseStatusList'
          }
        ]
      },
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 表格数据
      tableData: [{}],
      //  表格
      tableList: [
        { label: '设备名称', param: 'name', width: '130px' },
        { label: '设备编号', param: 'equipmentNo', width: '130px' },
        { label: 'IP', param: 'equipmentIp', width: '130px' },
        { label: 'MAC', param: 'equipmentMac', width: '150px' },
        { label: '资产类型', param: 'assetsTypeDictLable', width: '150px' },
        { label: '责任人', param: 'equipmentUserName', width: '150px' },
        { label: '责任人部门', param: 'equipmentOrgIdDictLable', width: '350px' },
        { label: '使用状态', param: 'equipmentUseStatusDictLable', width: '150px' },
        { label: '物理位置', param: 'equipmentStorage', width: '150px' }
      ],
      // 弹出层标题
      title: '',
      subBtn: false,
      // 部门树选项
      deptOptions: undefined,
      // 树结构参数
      defaultProps: {
        children: 'children',
        label: 'name'
      },

      // 查询参数
      queryParams: {
        size: 10,
        current: 1
      },
      // 列表规则
      dictList: {
        // 资产类型
        assetsAllTypeList: [],
        // 使用状态 空闲、领用中、借用中
        equipmentUseStatusList: [],
        // 部门
        orgList: [],
        // 设备类型
        equipmentTypeList: [],
        // 是否
        equipmentLocalizedList: [],

        // 设备类别
        equipmentCategoryList: [],

        // 联网类型
        equipmentHostNetTypeList: [],

        // 设备状态
        equipmentStatusList: [],

        // 涉密等级
        equipmentSecLevelList: [],

        // 重要程度
        assetsSecDominList: [],

        // 所在安全域
        equipmentCriticalityLevelList: [],

        // 操作系统名称
        osTypeList: [],

        assetsSecDomainAutoList: [],

        // 内置光驱类别
        equipmentBuiltincdDriveList: [],

        equipmentSecLevelAdjustList: [],

        equipmentPurposeList: [],

        // 内置光驱状态
        equipmentBuiltincdDrivestateList: [],

        // 设备类型
        equipmentSerTypeList: [],

        // 设备类别
        equipmentSerCategoryList: [],

        equipmentSerPurposeList: [],

        equipmentSerSpecList: [],

        // 联网类型
        equipmentSecNetTypeList: [],

        // 产品形态
        productStatesList: [],

        secproductList: [],

        qualificationTypeList: [],
        // 设备类型
        equipmentNetTypeList: [],

        // 设备类型
        equipmentMediumType: [],
        // 责任人
        equipmentUserIdList: [],
        // 责任部门
        equipmentOrgIdList: [],
        // 是否涉密专用设备
        equipmentSecretRelatedList: [],
        equipmentSecLevelAdjust: [],

        // 设备类型
        peripheralTypeList: [],
        // 使用范围
        equipmentScopeofuseList: [],

        // 设备类型
        otherEquipmentType: [],

        // 类型
        equipmentLocalized: [],

        // 部署服务器
        serverList: [],
        // 负载均衡设备
        loadBalancingList: [],
        // 存储设备
        storageDeviceList: [],
        // 数据库
        dataBaseList: [],
        // 中间件
        middleWareList: []
      }
    }
  },

  created() {
    this.getDict()
    this.getList()
    this.getTreeselect()
  },
  methods: {
    // 获取所有字典
    getDict() {
      return new Promise((resolve, reject) => {
        listAll('assetsAllType,equipmentUseStatus').then((res) => {
          for (const key in res) {
            this.dictList[key + 'List'] = res[key].map((item) => {
              return {
                value: item.itemVale,
                label: item.itemLabel
              }
            })
          }
          resolve(this.dictList)
        })
      })
    },
    /** 查询列表 */
    getList(val) {
      this.loading = true
      this.queryParams = { ...this.queryParams, ...val, scrollToTop: true }

      queryPageList(this.queryParams).then((res) => {
        this.total = res.total
        this.tableData = res.list
        this.loading = false
        this.queryParams.scrollToTop = false
      })
    },
    /** 查询部门下拉树结构 */
    getTreeselect(id) {
      entityTree(id).then((res) => {
        this.deptOptions = res
        this.dictList.orgList = res
        this.defaultExpandedKeys = [res[0].id]
      })
    },

    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.getList({ orgId: data.id })
    },

    /** 搜索按钮操作 */
    handleQuery(val) {
      this.queryParams.current = 1
      this.getList()
    },
    // 重置表单
    resetQuery() {
      for (const key in this.queryParams) {
        this.queryParams[key] = undefined
      }
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.getList()
    },
    // 搜索框变化
    handleEvent(val) {
      const field = val.field
      const value = val.value
      this.queryParams = {
        ...this.queryParams,
        ...{
          [field]: value
        }
      }
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.gDownload(
        window.baseUrl.asset + '/assetsAll/export',
        {
          ...this.queryParams
        },
        `全部设备_${new Date().getTime()}.xlsx`
      )
    },

    /** 详情按钮操作 */
    handleView(row) {
      this.title = '详情'
      switch (row.assetsType) {
        case 'assets_host':
          listAll(
            'equipmentType,equipmentLocalized,equipmentCategory,equipmentHostNetType,equipmentSecLevelAdjust,' +
              'equipmentStatus,equipmentUseStatus,equipmentSecLevel,assetsSecDomainAuto,equipmentBuiltincdDrive,' +
              'equipmentCriticalityLevel,osType,equipmentBuiltincdDrivestate,equipmentPurpose,equipmentNetType'
          ).then((res) => {
            for (const key in res) {
              this.dictList[key + 'List'] = res[key].map((item) => {
                return {
                  value: item.itemVale,
                  label: item.itemLabel
                }
              })
            }
            this.$refs.host.show(row.id)
          })
          break
        case 'assets_server':
          listAll(
            'equipmentSerType,equipmentLocalized,equipmentSerCategory,equipmentHostNetType,equipmentSecLevelAdjust,' +
              'equipmentStatus,equipmentUseStatus,equipmentSecLevel,assetsSecDomainAuto,equipmentBuiltincdDrive,' +
              'equipmentCriticalityLevel,osType,equipmentBuiltincdDrivestate,equipmentSerPurpose,equipmentSerSpec'
          ).then((res) => {
            for (const key in res) {
              this.dictList[key + 'List'] = res[key].map((item) => {
                return {
                  value: item.itemVale,
                  label: item.itemLabel
                }
              })
            }
            this.$refs.server.show(row.id)
          })
          break
        case 'assets_net_equipment':
          listAll(
            'equipmentNetType,equipmentLocalized,equipmentHostNetType,equipmentSecLevelAdjust,' +
              'equipmentStatus,equipmentUseStatus,equipmentSecLevel,assetsSecDomainAuto,' +
              'equipmentCriticalityLevel,osType,equipmentSerPurpose,equipmentSerSpec'
          ).then((res) => {
            for (const key in res) {
              this.dictList[key + 'List'] = res[key].map((item) => {
                return {
                  value: item.itemVale,
                  label: item.itemLabel
                }
              })
            }
            this.$refs.netDevice.show(row.id)
          })
          break
        case 'assets_sec_product':
          treeselectStorage().then((res) => {
            this.dictList.secproductList = res
          })
          listAll(
            'equipmentLocalized,equipmentSecNetType,productStates,equipmentStatus,equipmentUseStatus,equipmentSecLevel,' +
              'equipmentCriticalityLevel,equipmentSecLevelAdjust,qualificationType,equipmentSerPurpose'
          ).then((res) => {
            for (const key in res) {
              this.dictList[key + 'List'] = res[key].map((item) => {
                return {
                  value: item.itemVale,
                  label: item.itemLabel
                }
              })
            }
            this.$refs.secProductRef.show(row.id)
          })
          break
        case 'assets_storage_medium':
          listAll(
            'equipmentMediumType,equipmentLocalized,equipmentUseStatus,equipmentStatus,equipmentScopeOfUse,equipmentSecLevel,' +
              'equipmentCriticalityLevel,equipmentSecLevelAdjust,equipmentPurpose'
          ).then((res) => {
            for (const key in res) {
              this.dictList[key + 'List'] = res[key].map((item) => {
                return {
                  value: item.itemVale,
                  label: item.itemLabel
                }
              })
            }
            this.$refs.storageMedium.show(row.id)
          })
          break
        case 'assets_outsidee_equipment':
          listAll(
            'peripheralType,equipmentLocalized,equipmentNetType,equipmentUseStatus,equipmentStatus,equipmentSecLevel,' +
              'assetsSecDomainAuto,equipmentCriticalityLevel,equipmentLocalized,equipmentSecLevelAdjust,equipmentPurpose'
          ).then((res) => {
            for (const key in res) {
              this.dictList[key + 'List'] = res[key].map((item) => {
                return {
                  value: item.itemVale,
                  label: item.itemLabel
                }
              })
            }
            this.$refs.outsideeEquipment.show(row.id)
          })
          break
        case 'assets_other_equipment':
          listAll(
            'otherEquipmentType,equipmentLocalized,equipmentUseStatus,equipmentStatus,equipmentSecLevel,equipmentPurpose'
          ).then((res) => {
            for (const key in res) {
              this.dictList[key + 'List'] = res[key].map((item) => {
                return {
                  value: item.itemVale,
                  label: item.itemLabel
                }
              })
            }
            this.$refs.otherEquipment.show(row.id)
          })
          break
        case 'assets_software_system':
          treeselectStorageSystem().then((res) => {
            this.dictList.secproductList = res
          })
          queryServer('1,2,3').then((res) => {
            if (res.length > 0) {
              this.dictList.serverList = res.map((i) => {
                return { label: i.name, value: i.id }
              })
            } else {
              this.dictList.serverList = []
            }
          })

          queryLoadBalancing('1028730206514315278').then((res) => {
            if (res.length > 0) {
              this.dictList.loadBalancingList = res.map((i) => {
                return { label: i.name, value: i.id }
              })
            } else {
              this.dictList.loadBalancingList = []
            }
          })

          queryServer('4').then((res) => {
            if (res.length > 0) {
              this.dictList.storageDeviceList = res.map((i) => {
                return { label: i.name, value: i.id }
              })
            } else {
              this.dictList.storageDeviceList = []
            }
          })

          listAll(
            'storageSystemType,equipmentLocalized,equipmentHostNetType,equipmentUseStatus,equipmentStatus,equipmentSecLevel,' +
              'assetsSecDomainAuto,equipmentCriticalityLevel,equipmentSecLevelAdjust,equipmentPurpose,middleWare,dataBase'
          ).then((res) => {
            for (const key in res) {
              this.dictList[key + 'List'] = res[key].map((item) => {
                return {
                  value: item.itemVale,
                  label: item.itemLabel
                }
              })
            }
            this.$refs.softwareSystem.show(row.id)
          })

          break
        default:
          this.$message.warning('未找到资产信息')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.total-box {
  font-size: 14px;
  display: flex;
  width: 100%;
  height: 100%;
}
.head-container {
  overflow: auto;
  padding: 20px;
  height: 100%;
  width: 270px;
  border-right: 1px solid rgba(0, 0, 0, 0.08);
}
.app-container {
  // flex: 1;
  width: calc(100% - 270px);
}
.el-col {
  height: 100%;
  display: flex;
  flex-direction: column;
  &::v-deep .targetTable {
    flex: 1;
    overflow: auto;
  }
}
</style>
